import { Component,ViewChild ,Output} from '@angular/core';
import { CommonService } from '../service/common.service';
import { AlertController } from '@ionic/angular';
import * as EventEmitter from 'eventemitter3';

@Component({
  selector: 'app-tab1',
  templateUrl: 'tab1.page.html',
  styleUrls: ['tab1.page.scss']
})
export class Tab1Page {
  @Output() event = new EventEmitter();

  @ViewChild('slide2') slide2;
  @ViewChild('slide1') slide1;

  public bannerList: any[] = [];
  public noticeList: any[] = [];
  public newsLsit: any[] = [];
  public filecoin: any = {
  };
  public xchcoin: any = {
  };

  public ipfsData: any[] = [];
  public version = '1.0.0';

  public show=false;

  //轮播图的属性
  public slidesOpts={
    speed:400,
    passiveListeners: false,
    autoplay: {
      delay: 2000,
    },
    loop:true
  };

  public slidesOpts1={
    direction:'vertical',
    speed:400,
    passiveListeners: false,
    autoplay: {
      delay: 2000,
    },
    loop:true
  };

  constructor(private common: CommonService,private alertController: AlertController) {

  }

  // 刷新
  doRefresh(event) {
    setTimeout(() => {
      this.ngOnInit();
      this.slide2.startAutoplay();
      this.slide1.startAutoplay();
      // this.common.getIdInfo()
      event.target.complete();
    }, 1000);
  }

  // 初始化
  // eslint-disable-next-line @angular-eslint/use-lifecycle-interface
  ngOnInit(){
    // setTimeout(() => {
    //   this.show=false
    // }, 800);
    this.banners();
    this.notice();
    this.getNews();
    this.getCurrencyPriceList();
    this.getIpfsData();
    this.getAppVersion();
  }
  // 获取下载地址
  getAppVersion(){
    const api = '/tbr/getAppVersion';
    this.common.get(api).then((res: any)=>{
      console.log('获取下载地址',res);
      if(res.code === 200){
        if(res.data.ios_version !== this.version){
          this.presentAlertUrl();
        }
        // this.version = res.data.ios_version
      }
    });
  }
  // 滑动后，自主轮播
  slideTouchEnd(){
    // console.log(111)
    this.slide2.startAutoplay();
    this.slide1.startAutoplay();
  }

  // checkedBack(event) {
  //   console.log(event)
  // }

  // 通知消息，滚动播出
  notice(){
    const api = '/tbr/siteNotice/getList?page=1&size=20';
    this.common.get(api).then((res: any)=>{
      // console.log(res)
      if(res.code === 200){
        this.noticeList = res.data;
      }
    });
  }

  // 1 首页 2商城 3咨询
  banners(){
    const api = '/tbr/getBanners';
    const params = {type:1};
    this.common.post(api,params,false).then((res: any)=>{

      if(res.code === 200){
        this.bannerList = res.data;
        // console.log(this.bannerList)
        this.event.emit(this.bannerList[0].imagePath);
      }
    });
  }

  // 获取币价
  getCurrencyPriceList(){
    const api = '/tbr/index/getCurrencyPriceList';
    this.common.get(api).then((res: any)=>{
      console.log(res);
      if(res.code === 200){
        this.filecoin = res.data.filMarket;
        this.xchcoin = res.data.xchMarket;
        this.filecoin.sheng = (((this.filecoin.close - this.filecoin.open)/this.filecoin.open)*100).toFixed(2);
        this.xchcoin.sheng = (((this.xchcoin.close - this.xchcoin.open)/this.xchcoin.open)*100).toFixed(2);
        // this.filecoin.sheng = 0.01
        if(this.filecoin.sheng < 0){
          this.filecoin.sign = true;
        }else{
          this.filecoin.sign = false;
        }

        if(this.xchcoin.sheng < 0){
          this.xchcoin.sign = true;
        }else{
          this.xchcoin.sign = false;
        }
      }
    });
  }

  // 我的ipfs矿机
  getIpfsData(){
    const api = '/tbr/index/getMyMiner';
    this.common.get(api).then((res: any)=>{
      console.log('我的ipfs矿机');
      console.log(res);
      if(res.code === 200){
        this.ipfsData = res.data;
      }
    });
  }

  // 首页行业快讯
  getNews(){
    const api = '/tbr/getInformationFrist';
    this.common.post(api,null,false).then((res: any)=>{
      // console.log(res)
      if(res.code === 200){
        this.newsLsit = res.data;
      }
    });
  }

  // 弹出框
  async presentAlertUrl() {
    const alert = await this.alertController.create({
        // cssClass: 'my-custom-class',
        header: '提示',
        // subHeader: 'Subtitle',
        message: '请下载最新版本',
        buttons: [
          {
            text: '确定',
            handler: (blah) => {
              window.location.href = this.common.configs + '/invite/download.html';
            }
          }
        ]
    });
    await alert.present();
  }

}
